package com.game.spacetrader;

import com.game.spacetrader.TradeGoods.TradeGoods;
import com.game.spacetrader.TradeGoods.TradeGoodsAdaptor;
import com.game.spacetrader.TradeGoods.TradeGoodsView;
import com.game.spacetrader.map.Coordinates;
import com.game.spacetrader.map.MapView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * View is a class to represent a planet that can be traveled to
 *
 */
public class PlanetView extends Activity implements OnClickListener{
	private TextView planetName, location, distance, techLevel, resourceLevel;
	private Button travel;
	private double d;
	private Player player;
	private Game game;
	private RandomEvent event;

	/**
	 * Creates the view
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.planet_view);

		planetName = (TextView)findViewById(R.id.planetName);
		location = (TextView)findViewById(R.id.location);
		distance = (TextView)findViewById(R.id.distance);
		techLevel = (TextView)findViewById(R.id.techLevel);
		resourceLevel = (TextView)findViewById(R.id.resourceLevel);

		travel = (Button)findViewById(R.id.travel);
		travel.setOnClickListener(this);
		game = Game.getInstance();
		player = game.getPlayer();
		setTextViews();
	}

	/**
	 * Sets the text for the TextView
	 */
	public void setTextViews(){
		Planet p = game.getTravelTo();
		planetName.setText(p.getName());
		Coordinates coord = p.getCoord();
		Coordinates playerC = player.getLocation();
		location.setText("Location: ("+coord.getX()+", "+coord.getY()+")");
		d = Math.sqrt(Math.pow(coord.getX()-playerC.getX(),2)+Math.pow(coord.getY()-playerC.getY(),2));
		distance.setText("Distance: "+d);
		techLevel.setText("Tech Level: "+p.getTechLevel());
		resourceLevel.setText("Resource Level: "+p.getResources());
	}

	/**
	 * Handles any clicking on the screen
	 */
	public void onClick(View arg0) {
		switch(arg0.getId()){
		case R.id.travel:
			if(player.getGas()>d){
				boolean b = game.travel(game.getTravelTo(), d);
				if(!b){
					Log.d("PlanetView travel", "travel failed");
					Toast.makeText(getApplicationContext(), "You do not have enough gas.",	Toast.LENGTH_LONG).show();
				}
				else{
					Log.d("PlanetView travel", "travel success");
					goToMarket(game.getTravelTo());
				}
			}
			break;
		}

	}

	/**
	 * Goes to the market place screen for the inputted planet
	 * @param p
	 */
	public void goToMarket(Planet p) {
		TradeGoodsAdaptor.getAllGoods(p);
		for(TradeGoods i:TradeGoodsAdaptor.goods){
			//			if(p.getTechLevel()<i.getMtlp())
			//				i.setQuantityHave(0);
			//			else
			i.calcQuantity(p);
		}
		player.setPlanet(p);
		Intent i = new Intent(this, TradeGoodsView.class);
		startActivity(i);
	}

	/**
	 * Creates the option menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_space_trader, menu);
		return true;
	}

	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}
	
	/**
     * Listener for the options menu
     * @param item the item in the Menu that is clicked
     */
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.travel:
	        	Intent i = new Intent(this, MapView.class);
	        	startActivity(i);
	            return true;
	        case R.id.market:
	        	Intent i2 = new Intent(this, TradeGoodsView.class); startActivity(i2);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
    
    /**
	 * Handles if a key is pressed down
	 * @param keyCode 
	 * @param event
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			this.finish();
	    	return true;
	    }
	    else 
	    	return false ;
	}
}
